package top.ytazwc.improve.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import top.ytazwc.improve.service.ICommodity;
import top.ytazwc.store.coupon.CouponResult;
import top.ytazwc.store.coupon.CouponService;

import java.util.Map;

/**
 * @author 花木凋零成兰
 * @title CouponCommodityService
 * @date 2024/4/28 11:05
 * @package top.ytazwc.improve.service.impl
 * @description 发放优惠卷 服务
 */
@Slf4j
public class CouponCommodityService implements ICommodity {
    // 模拟依赖注入
    private CouponService couponService = new CouponService();
    @Override
    public void sendCommodity(String userId, String commodityId, String bizId, Map<String, String> extMap) throws Exception {
        // 执行发放优惠卷
        CouponResult couponResult = couponService.sendCoupon(userId, commodityId, bizId);
        log.info("请求参数[优惠券] => uId：{} commodityId：{} bizId：{} extMap：{}", userId, commodityId, bizId, JSON.toJSON(extMap));
        log.info("测试结果[优惠券]：{}", JSON.toJSON(couponResult));
        if (!"0000".equals(couponResult.getCode())) {
            // 若发放奖品失败 抛出异常
            throw new RuntimeException(couponResult.getInfo());
        }
    }
}
